<?php
/**
 * 收货地址验证器
 */
namespace app\api\validate;

use think\Validate;
use think\Db;

class WalletValidate extends Validate
{
    protected $rule = [
        'card_id'     => ['require'],
        'money'     => ['require'],
        'card_id'     => ['require', 'checkCard'],
        'money'     => ['require', 'checkBalance'],
        'pay_password'       => 'require',
        'pay_password'     => ['require', 'checkPayPassword'],
    ];

    protected $message = [
        'card_id.require' => '请选择银行卡',
        'money.require' => '请输入提现金额',
        'pay_password.require' => '请输入支付密码',
    ];

    function checkCard($value,$rule,$data){
        $credit = Db::name('credit')->where('credit_id = "'.$data['card_id'].'" and user_id = "'.$data['user_id'].'" and is_del = 1')->find();
        if(!$credit){
            return '请选择正确的银行卡';
        }
        return true;
    }

    function checkBalance($value,$rule,$data){
        if($data['balance'] < $data['money']){
            return '您的余额不足';
        }
        return true;
    }

    public function checkPayPassword($value, $rule ,$data)
    {
        if(!$data['pay_code']){
            return '您尚未设置支付密码';
        }
        if(!password_verify($data['pay_password'],$data['pay_code'])){
            return '您的支付密码错误';
        }
		return true;
    }

}